package com.lqq.chapter13.Atomic;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Author lqq677
 * @Date 2021/8/13 0:23
 * @Description 原子更新引用类型
 */
public class AtomicReferenceTest {

    static class User {
        private String name;
        private int old;

        public User(String name, int old) {
            this.name = name;
            this.old = old;
        }

        public String getName() {
            return name;
        }

        public int getOld() {
            return old;
        }
    }

    public static AtomicReference<User> atomicUserRef = new AtomicReference<User>();


    public static void main(String[] args) {
        User user = new User("conan", 15);

        atomicUserRef.set(user);
        User updateUser = new User("lqq677", 22);
        atomicUserRef.compareAndSet(user, updateUser);

        System.out.println(atomicUserRef.get().getName());//lqq677
        System.out.println(atomicUserRef.get().getOld());//22
    }

}
